package com.youedata.modular.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youedata.base.pojo.page.LayuiPageFactory;
import com.youedata.base.tips.DCResponse;
import com.youedata.modular.entity.ArchivalInformation;
import com.youedata.modular.model.dto.ArchivalInformationDTO;
import com.youedata.modular.service.IArchivalInformationService;
import com.youedata.modular.service.IUserService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.sys.modular.system.entity.User;
import com.youedata.sys.modular.system.service.UserService;
import com.youedata.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.ObjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.beans.BeanUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 档案信息表控制器
 *
 * @author hao.yan
 * @date 2021-09-04 16:07:24
 */
@Controller
@Api(value = "/archivalInformation", tags = "档案信息表管理")
@RequestMapping("/archivalInformation")
public class ArchivalInformationController {

    private String PREFIX = "/archivalInformation";

    @Autowired
    private IArchivalInformationService archivalInformationService;

    @Autowired
    private UserService userService;

    /**
     * 新增
     *
     * @author hao.yan
     * @date 2021-09-04
     */
    @ResponseBody
    @PostMapping("/addItem")
    @ApiOperation(value = "新增")
    public DCResponse<Void> addItem(ArchivalInformationDTO archivalInformationDto) {
        ArchivalInformation archivalInformation = new ArchivalInformation();
        BeanUtils.copyProperties(archivalInformationDto, archivalInformation);
        archivalInformation.setCreateUser(UserHolder.getUserId());
        archivalInformation.setCreateTime(LocalDateTime.now());
        this.archivalInformationService.save(archivalInformation);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键删除
     *
     * @author hao.yan
     * @date 2021-09-04
     */
    @ResponseBody
    @PostMapping("/delete")
    @ApiOperation("删除")
    public DCResponse<Void> delete(@RequestParam("id") String id) {
        this.archivalInformationService.removeById(id);
        return DCResponse.ok(null);
    }

    /**
     * 修改
     *
     * @author hao.yan
     * @date 2021-09-04
     */
    @ResponseBody
    @PostMapping("/editItem")
    @ApiOperation("根据ID修改信息")
    public DCResponse<Void> editItem(ArchivalInformationDTO archivalInformationDto) {
        ArchivalInformation archivalInformation = new ArchivalInformation();
        BeanUtils.copyProperties(archivalInformationDto, archivalInformation);
        archivalInformation.setUpdateUser(UserHolder.getUserId());
        archivalInformation.setUpdateTime(LocalDateTime.now());
        this.archivalInformationService.updateById(archivalInformation);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键查询单条详情
     *
     * @author hao.yan
     * @date 2021-09-04
     */
    @ResponseBody
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键查询单条详情")
    public ResponseData detail(@RequestParam("id") Long id) {
        Map<String, Object> result = this.archivalInformationService.detailById(id);
        return ResponseData.success(result);
    }

    /**
     * 分页查询列表
     *
     * @author hao.yan
     * @date 2021-09-04
     */
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, defaultValue = "1", paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, defaultValue = "10", paramType = "query", dataType = "int", example = "20")
    })
    public Object list(ArchivalInformationDTO archivalInformationDto) {
        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //根据条件查询
        page.setRecords(this.archivalInformationService.pageSimpleInfo(page, archivalInformationDto));

        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到主页面
     *
     * @author hao.yan
     * @Date 2021-09-04
     */
    @RequestMapping()
    public String index() {
        return PREFIX + "/archivalInformation.html";
    }

    /**
     * 新增页面
     *
     * @author hao.yan
     * @Date 2021-09-04
     */
    @RequestMapping("/add")
    public String add(Model model) {

        List<Map<String, Object>> workers = this.userService.listMaps(
                new QueryWrapper<User>().select("user_id as userId,name"));

        if (ToolUtil.isNotEmpty(workers)) {
            model.addAttribute("workers", workers);
        }
        return PREFIX + "/archivalInformation_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hao.yan
     * @Date 2021-09-04
     */
    @RequestMapping("/edit")
    public String edit(Model model) {

        List<Map<String, Object>> workers = this.userService.listMaps(
                new QueryWrapper<User>().select("user_id as userId,name"));

        if (ToolUtil.isNotEmpty(workers)) {
            model.addAttribute("workers", workers);
        }
        return PREFIX + "/archivalInformation_edit.html";
    }

}
